import dt from '@dt/dt'
import Opt from '@/components/dt/info/Opt.js'
import dateOpt from '@/biz/option/date.js'

class Column extends Opt {
  constructor() {
    super()
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
      {
        field: 'companyName',
        title: '所属组织',
        minWidth: 150
      },
      {
        field: 'housesName',
        title: '案场名称',
        minWidth: 160
      },
      {
        field: 'commissionName',
        title: '佣金单名称',
        minWidth: 200,
        header: {
          filter: {
            key: 'commissionName',
            title: '佣金单名称',
            placeholder: '请输入佣金单名称',
            input: {}
          }
        }
      },
      {
        field: 'commissionMonth',
        title: '结算月份',
        minWidth: 120,
        header: {
          filter: {
            key: 'commissionStartMonth',
            title: '结算月份',
            width: 270,
            dtTime: {
              dateType: 'month',
              dateFormat: 'YYYY-MM',
              transfer: false,
              clearable: true
            },
            filter: {
              check: (val) => {
                if (dt.tool.dateDiff(val.commissionStartMonth, val.commissionEndMonth).Y >= 2) {
                  // 日期最大跨度两年
                  dt.ui.Message.warning('结算月份跨度至多两年。')
                }
                return !(dt.tool.dateDiff(val.commissionStartMonth, val.commissionEndMonth).Y >= 2)
              }
            },
            data: {
              from: (info) => {
                if (info.commissionStartMonth) {
                  return [info.commissionStartMonth, info.commissionEndMonth]
                } else {
                  return null
                }
              },
              to: (val) => {
                return {
                  commissionStartMonth: val[0],
                  commissionEndMonth: val[1]
                }
              }
            }
          }
        }
      },
      {
        field: 'settlementType',
        title: '结算类型',
        minWidth: 150,
        cell: {
          text: {
            type: 'dict',
            key: 'settlementType'
          }
        },
        header: {
          filter: {
            key: 'settlementType',
            title: '结算类型',
            select: {
              key: 'settlementType',
              clearable: true
            }
          }
        }
      },
      {
        field: 'status',
        title: '结算状态',
        minWidth: 100,
        cell: {
          text: {
            type: 'dict',
            key: 'commissionStatusTWo'
          }
        }
      },
      {
        field: 'voucherStatusStr',
        title: '确认凭证',
        minWidth: 100,
        cell: {
          text: {}
        },
        header: {
          filter: {
            key: 'voucherStatus',
            title: '确认凭证',
            select: {
              options: [
                { key: '1', value: '已上传' },
                { key: '2', value: '未上传' }
              ],
              clearable: true
            }
          }
        }
      },
      {
        field: 'invoicingStatus',
        title: '开票状态',
        minWidth: 100,
        cell: {
          value: (row) => dt.dictInfo().__value('invoicingStatus', row.invoicingStatus),
          text: {}
        },
        header: {
          filter: {
            key: 'invoicingStatus',
            title: '开票状态',
            select: {
              key: 'invoicingStatus',
              clearable: true
            }
          }
        }
      },
      {
        field: 'isUploadInvoiceReceipt',
        title: '发票回执',
        minWidth: 100,
        header: {
          filter: {
            key: 'isUploadInvoiceReceipt',
            title: '发票回执',
            placeholder: '全部',
            select: {
              options: [
                { key: 1, value: '已上传' },
                { key: 0, value: '未上传' }
              ]
            },
            data: {
              from: (info) =>
                info.isUploadInvoiceReceipt ? '1' : info.isUploadInvoiceReceipt == false ? '0' : null,
              to: (val) => ({ isUploadInvoiceReceipt: val.value == 1 })
            },
            clearable: true
          }
        }
      },
      {
        field: 'settlementStatus',
        title: '核销状态',
        minWidth: 100,
        cell: {
          value: (row) => dt.dictInfo().__value('WriteOffStatus', row.settlementStatus),
          text: {}
        },
        header: {
          filter: {
            key: 'settlementStatuses',
            title: '核销状态',
            select: {
              key: 'WriteOffStatus',
              clearable: true,
              multiple: true
            }
          }
        }
      },
      {
        field: 'accountingCompany',
        title: '入账公司',
        minWidth: 200,
        header: {
          filter: {
            key: 'accountingCompany',
            title: '入账公司',
            placeholder: '请输入入账公司',
            input: {}
          }
        }
      },
      {
        field: 'sumAmount',
        title: '总签约金额(元)',
        minWidth: 150,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        field: 'settlementSumSale',
        title: '本次结算总销(元)',
        minWidth: 150,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        field: 'preCommissionAmount',
        title: '本次应结佣金(元)',
        minWidth: 150,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        field: 'writtenOffAmount',
        title: '已核销佣金(元)',
        minWidth: 150,
        cell: {
          text: {
            type: 'number'
          }
        }
      },

      {
        field: 'relationBonusName',
        title: '关联奖金单',
        minWidth: 180
      },
      {
        title: '操作',
        width: 350,
        fixed: 'right',
        cell: (row) => {
          return {
            btn: [
              {
                title: '佣金结算',
                event: 'onSettlement',
                code: '0210020100',
                visible:
                  row.status == 'NO_SETTLEMENT' ||
                  (row.status == 'SETTLED' && row.settlementStatus != 'WRITTEN_OFF')
              },
              {
                title: '佣金明细',
                event: 'onDetail',
                code: '0210020500',
                visible:
                  row.status == 'BACK' ||
                  row.status == 'INVALIDED' ||
                  (row.status == 'SETTLED' && row.settlementStatus == 'WRITTEN_OFF')
              },
              {
                title: '回款信息',
                event: 'onPayment',
                code: '0210020600',
                visible: row.status == 'SETTLED'
              },
              {
                title: '确认凭证',
                event: 'onConfirm',
                code: '0210020400',
                visible: row.status == 'SETTLED' && row.voucherStatusStr == '未上传'
              },
              {
                title: '发票回执',
                event: 'onInvoiceReceipt',
                code: '0210020800',
                visible:
                  row.status == 'SETTLED' &&
                  row.invoicingStatus == 'INVOICED' &&
                  row.isUploadInvoiceReceipt == '未上传'
              },
              {
                title: '开票',
                event: 'onInvoice',
                code: '0210020200',
                visible: row.status == 'SETTLED' && row.invoicingStatus == 'NO_INVOICE'
              },
              {
                title: '核销',
                event: 'onWriteoff',
                code: '0210020300',
                visible: row.status == 'SETTLED' && row.settlementStatus != 'WRITTEN_OFF'
              }
            ]
          }
        }
      }
    ]
  }
}

export default Column
